package MyGui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowEvent;

import DistSys.*;
import Element.*;

public class ActiveTempDlg extends JDialog implements WindowListener {
    private JComboBox[] combosTemp;

    public ActiveTempDlg(MainFrame parent, String title, boolean b) {
        super(parent, title, b);
        setSize(600,350);
        setLocation(200,200);
        //setResizable(false);
        addWindowListener( this );

        int rows = (SysConst.ssType.length-2 + 1) / 2;
        JPanel editPanel = new JPanel(new GridLayout(rows, 4, 5, 5));
        getContentPane().setLayout( new BorderLayout(5,5));
        getContentPane().add(editPanel);

        combosTemp = new JComboBox[SysConst.MAX_TYPE];
        combosTemp[SysConst.TYPE_LINE] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_LINE]);
        combosTemp[SysConst.TYPE_LINE].setSelectedItem(Line.objActiveTemp.getElemGeomObj().getName());

        combosTemp[SysConst.TYPE_CABLE] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_CABLE]);
        combosTemp[SysConst.TYPE_CABLE].setSelectedItem(Line.objActiveTemp2.getElemGeomObj().getName());

        combosTemp[SysConst.TYPE_LOAD] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_LOAD]);
        combosTemp[SysConst.TYPE_LOAD].setSelectedItem(Load.objActiveTemp.getElemGeomObj().getName());
        combosTemp[SysConst.TYPE_CAPACITOR] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_LOAD]);
        combosTemp[SysConst.TYPE_CAPACITOR].setSelectedItem(Cap.objActiveTemp.getElemGeomObj().getName());
        combosTemp[SysConst.TYPE_DIST_GEN] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_LOAD]);
        combosTemp[SysConst.TYPE_DIST_GEN].setSelectedItem(DG.objActiveTemp.getElemGeomObj().getName());
        combosTemp[SysConst.TYPE_MOTOR] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_LOAD]);
        combosTemp[SysConst.TYPE_MOTOR].setSelectedItem(Motor.objActiveTemp.getElemGeomObj().getName());

        combosTemp[SysConst.TYPE_BREAKER] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_PROTECTION]);
        combosTemp[SysConst.TYPE_BREAKER].setSelectedItem(Breaker.objActiveTemp.getElemGeomObj().getName());
        combosTemp[SysConst.TYPE_DRT_BREAKER] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_PROTECTION]);
        combosTemp[SysConst.TYPE_DRT_BREAKER].setSelectedItem(DrtBreaker.objActiveTemp.getElemGeomObj().getName());
        combosTemp[SysConst.TYPE_FUSE] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_PROTECTION]);
        combosTemp[SysConst.TYPE_FUSE].setSelectedItem(Fuse.objActiveTemp.getElemGeomObj().getName());
        combosTemp[SysConst.TYPE_SECTIONIZER] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_PROTECTION]);
        combosTemp[SysConst.TYPE_SECTIONIZER].setSelectedItem(Sect.objActiveTemp.getElemGeomObj().getName());
        combosTemp[SysConst.TYPE_ARRESTER] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_PROTECTION]);
        combosTemp[SysConst.TYPE_ARRESTER].setSelectedItem(Arrester.objActiveTemp.getElemGeomObj().getName());
        combosTemp[SysConst.TYPE_RECLOSER] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_PROTECTION]);
        combosTemp[SysConst.TYPE_RECLOSER].setSelectedItem(Recloser.objActiveTemp.getElemGeomObj().getName());

        combosTemp[SysConst.TYPE_SOURCE] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_SOURCE]);
        combosTemp[SysConst.TYPE_SOURCE].setSelectedItem(Source.objActiveTemp.getElemGeomObj().getName());

        combosTemp[SysConst.TYPE_SWITCH] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_SWITCH]);
        combosTemp[SysConst.TYPE_SWITCH].setSelectedItem(Switch.objActiveTemp.getElemGeomObj().getName());
        combosTemp[SysConst.TYPE_SWITCH3] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_SWITCH]);
        combosTemp[SysConst.TYPE_SWITCH3].setSelectedItem(Switch3.objActiveTemp.getElemGeomObj().getName());
        combosTemp[SysConst.TYPE_SWITCH_GND] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_SWITCH]);
        combosTemp[SysConst.TYPE_SWITCH_GND].setSelectedItem(SwitchGnd.objActiveTemp.getElemGeomObj().getName());

        combosTemp[SysConst.TYPE_TRANSFORMER] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_TRANSFORMER]);
        combosTemp[SysConst.TYPE_TRANSFORMER].setSelectedItem(Txfm.objActiveTemp.getElemGeomObj().getName());
        combosTemp[SysConst.TYPE_REGULATOR] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_TRANSFORMER]);
        combosTemp[SysConst.TYPE_REGULATOR].setSelectedItem(Regulator.objActiveTemp.getElemGeomObj().getName());
        combosTemp[SysConst.TYPE_CURR_TX] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_TRANSFORMER]);
        combosTemp[SysConst.TYPE_CURR_TX].setSelectedItem(CT.objActiveTemp.getElemGeomObj().getName());
        combosTemp[SysConst.TYPE_VOLT_TX] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_TRANSFORMER]);
        combosTemp[SysConst.TYPE_VOLT_TX].setSelectedItem(PT.objActiveTemp.getElemGeomObj().getName());
        combosTemp[SysConst.TYPE_3W_TX] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_TRANSFORMER]);
        combosTemp[SysConst.TYPE_3W_TX].setEnabled(false);

        combosTemp[SysConst.TYPE_BUS] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_MISC]);
        combosTemp[SysConst.TYPE_BUS].setSelectedItem(Bus.objActiveTemp.getElemGeomObj().getName());
        combosTemp[SysConst.TYPE_SERIES_CAP] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_MISC]);
        combosTemp[SysConst.TYPE_SERIES_CAP].setSelectedItem(SeriesCap.objActiveTemp.getElemGeomObj().getName());
        combosTemp[SysConst.TYPE_REACTOR] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_MISC]);
        combosTemp[SysConst.TYPE_REACTOR].setSelectedItem(Reactor.objActiveTemp.getElemGeomObj().getName());
        combosTemp[SysConst.TYPE_GROUP] = new JComboBox(Templates.nameLists[SysConst.TEMPLTYPE_MISC]);
        combosTemp[SysConst.TYPE_GROUP].setSelectedItem(Group.objActiveTemp.getElemGeomObj().getName());

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < 2; j++) {
                if ((i + j*rows + 2) < SysConst.ssType.length) {
                    editPanel.add(new JLabel(SysConst.ssType[i + j*rows + 2],
                                             JLabel.RIGHT));
                    editPanel.add(combosTemp[SysConst.mapIndexToType(i + j*rows + 2)]);
                }
            }
        }
    }

    /*=====================================*
     * Implement WindowListener interfaces *
     *=====================================*/
    public void windowOpened( WindowEvent evt ){ }
    public void windowClosed( WindowEvent evt ){ }
    public void windowIconified( WindowEvent evt ){ }
    public void windowDeiconified( WindowEvent evt ){ }
    public void windowActivated( WindowEvent evt ){ }
    public void windowDeactivated( WindowEvent evt ){ }

    private AbsElem getDftElem(int templType, int type) {
        return (AbsElem)Templates.subLists[templType].elementAt(combosTemp[type].
            getSelectedIndex());
    }

    public void windowClosing( WindowEvent evt ){
        Line.objActiveTemp = getDftElem(SysConst.TEMPLTYPE_LINE, SysConst.TYPE_LINE);

        Line.objActiveTemp2 = getDftElem(SysConst.TEMPLTYPE_CABLE, SysConst.TYPE_CABLE);

        Load.objActiveTemp  = getDftElem(SysConst.TEMPLTYPE_LOAD, SysConst.TYPE_LOAD);
        Cap.objActiveTemp   = getDftElem(SysConst.TEMPLTYPE_LOAD, SysConst.TYPE_CAPACITOR);
        DG.objActiveTemp    = getDftElem(SysConst.TEMPLTYPE_LOAD, SysConst.TYPE_DIST_GEN);
        Motor.objActiveTemp = getDftElem(SysConst.TEMPLTYPE_LOAD, SysConst.TYPE_MOTOR);

        Breaker.objActiveTemp    = getDftElem(SysConst.TEMPLTYPE_PROTECTION, SysConst.TYPE_BREAKER);
        DrtBreaker.objActiveTemp = getDftElem(SysConst.TEMPLTYPE_PROTECTION, SysConst.TYPE_DRT_BREAKER);
        Fuse.objActiveTemp       = getDftElem(SysConst.TEMPLTYPE_PROTECTION, SysConst.TYPE_FUSE);
        Sect.objActiveTemp       = getDftElem(SysConst.TEMPLTYPE_PROTECTION, SysConst.TYPE_SECTIONIZER);
        Arrester.objActiveTemp   = getDftElem(SysConst.TEMPLTYPE_PROTECTION, SysConst.TYPE_ARRESTER);
        Recloser.objActiveTemp   = getDftElem(SysConst.TEMPLTYPE_PROTECTION, SysConst.TYPE_RECLOSER);

        Source.objActiveTemp = getDftElem(SysConst.TEMPLTYPE_SOURCE, SysConst.TYPE_SOURCE);

        Switch.objActiveTemp    = getDftElem(SysConst.TEMPLTYPE_SWITCH, SysConst.TYPE_SWITCH);
        Switch3.objActiveTemp   = getDftElem(SysConst.TEMPLTYPE_SWITCH, SysConst.TYPE_SWITCH3);
        SwitchGnd.objActiveTemp = getDftElem(SysConst.TEMPLTYPE_SWITCH, SysConst.TYPE_SWITCH_GND);

        Txfm.objActiveTemp      = getDftElem(SysConst.TEMPLTYPE_TRANSFORMER, SysConst.TYPE_TRANSFORMER);
        Regulator.objActiveTemp = getDftElem(SysConst.TEMPLTYPE_TRANSFORMER, SysConst.TYPE_REGULATOR);
        CT.objActiveTemp        = getDftElem(SysConst.TEMPLTYPE_TRANSFORMER, SysConst.TYPE_CURR_TX);
        PT.objActiveTemp        = getDftElem(SysConst.TEMPLTYPE_TRANSFORMER, SysConst.TYPE_VOLT_TX);
        //3WindingTxfm.activeTemp = getDftElem(SysConst.TEMPLTYPE_TRANSFORMER, SysConst.TYPE_3W_TX);

        Bus.objActiveTemp       = getDftElem(SysConst.TEMPLTYPE_MISC, SysConst.TYPE_BUS);
        SeriesCap.objActiveTemp = getDftElem(SysConst.TEMPLTYPE_MISC, SysConst.TYPE_SERIES_CAP);
        Reactor.objActiveTemp   = getDftElem(SysConst.TEMPLTYPE_MISC, SysConst.TYPE_REACTOR);
        Group.objActiveTemp     = getDftElem(SysConst.TEMPLTYPE_MISC, SysConst.TYPE_GROUP);
    } // end of windowClosing()
}